home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: checkDeadlock.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:53 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
-
- #include "sysdefs.h"
- #include "ess.h"
- #include "checking.h"
- #include "trace.h"
- #include "error.h"
- #include "list.h"
- #include "pool.h"
- #include "tid.h"
- #include "io.h"
- #include "lock.h"
- #include "object.h"
- #include "msgdefs.h"
- #include "thread.h"
- #include "semaphore.h"
- #include "link.h"
- #include "lsn.h"
- #include "latch.h"
- #include "bf.h"
- #include "volume.h"
- #include "trans.h"
- #include "lm_intfuncs.h"
- #include "lm_extfuncs.h"
- #include "lm_globals.h"
-
- #define TRAILMAX 10
-
-
- int
- checkDeadlock (
-
- register TRANSREC *targetTrans,
- register LOCKENTRY *holderEntry
- )
- {
-
- register LOCKENTRY *lockEntry;
- register TRANSREC *holderTrans;
- register LOCKENTRY **top;
- LOCKENTRY* DLtrail[TRAILMAX];
- int DLtrailCnt = 0;
- int detected = FALSE;
-
- DLtrail[DLtrailCnt++] = holderEntry;
-
-
- TRPRINT(TR_LOCK, TR_LEVEL_1, ("tid:%x", GETTID(targetTrans)));
-
- /*
- * increment the visited count
- */
- DeadlockVisitCount++;
-
- /*
- * the holder entry must me non-null
- */
- CHECK_LOCKENTRY_MAGIC(holderEntry);
-
- /*
- * loop until all reachable nodes have been visited
- */
- for (top = &(DeadlockStack[0]); ! detected;) {
-
- /*
- * search down the list
- */
- while (holderEntry != NULL) {
-
- TRPRINT(TR_LOCK, TR_LEVEL_2, ("lockid:%x:%x:%x",
- holderEntry->lockHeader->hashList.lockid.lockid.word1,
- holderEntry->lockHeader->hashList.lockid.lockid.word2,
- holderEntry->lockHeader->hashList.lockid.lockid.word3));
-
- CHECK_LOCKENTRY_MAGIC(holderEntry);
-
- holderTrans = holderEntry->headerList.transRec;
- CHECK_TRANSREC_MAGIC(holderTrans);
- TRPRINT(TR_LOCK, TR_LEVEL_2,
- ("holder tid:%x", GETTID(holderTrans)));
-
- if (holderTrans == targetTrans) {
-
- /*
- * we have gotten back so have deadlock
- */
- detected = TRUE;
- break;
- }
-
-
- if (NOT_VISITED(holderTrans)) {
-
- TRPRINT(TR_LOCK, TR_LEVEL_2, ("transaction not visited"));
-
- SET_VISITED(holderTrans);
-
- /*
- * check to see if the transaction is in lock wait
- */
- if (LIST_NOT_EMPTY( &(holderTrans->lockWaitList) ) ||
- LIST_NOT_EMPTY( &(holderTrans->lockUpgradeList) )) {
-
- TRPRINT(TR_LOCK, TR_LEVEL_2, ("transaction in lock wait"));
-
- SM_ASSERT(LEVEL_3,
- top < &(DeadlockStack[DEAD_STACK_SIZE - 1]));
-
- /*
- * push the holder entry on the stack
- */
- *(top++) = (LOCKENTRY *)
- NEXT_LIST_ELEMENT(&(holderEntry->headerList.list));
-
- /*
- * check to see if this is a wait or an upgrade
- */
- if (LIST_NOT_EMPTY( &(holderTrans->lockWaitList))) {
- lockEntry = (LOCKENTRY*)
- FIRST_LIST_ELEMENT(&(holderTrans->lockWaitList));
- } else {
- /*
- * Upgrade entries contain are identical to
- * other entries except that the lockHeader
- * pointer points to the lock entry of the
- * held lock instead of the actuall lock
- * header.
- */
- lockEntry = (LOCKENTRY*) (
- ((LOCKENTRY*) FIRST_LIST_ELEMENT(&(holderTrans->lockUpgradeList)))->lockHeader);
- }
- CHECK_LOCKENTRY_MAGIC(lockEntry);
-
- /*
- * get a pointer to the list of lock holders
- */
- CHECK_LOCKHEADER_MAGIC(lockEntry->lockHeader);
- holderEntry = (LOCKENTRY *) NEXT_LIST_ELEMENT(
- &(lockEntry->lockHeader->grantedList) );
- CHECK_LOCKHEADER_MAGIC(holderEntry->lockHeader);
-
- DLtrail[DLtrailCnt++] = holderEntry;
- SM_ASSERT(LEVEL_1, DLtrailCnt <= TRAILMAX);
- continue;
- }
- }
-
- /*
- * look at the next element in the list
- */
- holderEntry = (LOCKENTRY *) NEXT_LIST_ELEMENT(
- &(holderEntry->headerList.list));
- }
-
- /*
- * check if we have traversed all nodes
- */
- if (top > DeadlockStack)
- holderEntry = * (--top); /* pop off the stack */
- else
- break;
- }
-
- if (detected) {
- /*
- * deadlock detected !!
- */
- int pageLockOnly = TRUE;
- int i;
- LOCKID* lockIdPtr;
- fprintf(sm_ErrorStream, "SERVER: deadlock detected -- aborting a transaction.\n");
- fprintf(sm_ErrorStream, " %d locks are involved in the deadlock: \n", DLtrailCnt);
- fprintf(sm_ErrorStream, " TRANS LOCK\n");
- fprintf(sm_ErrorStream, " ------- -----------------\n");
- for (i = 0; i < DLtrailCnt; i++) {
- holderEntry = DLtrail[i];
- holderTrans = holderEntry->headerList.transRec;
- lockIdPtr = & holderEntry->lockHeader->hashList.lockid;
- fprintf(sm_ErrorStream, " %3d %s\n", holderTrans->tid,
- printLockId(*lockIdPtr));
- if (lockIdPtr->page.type != PAGE_LOCK)
- pageLockOnly = FALSE;
- }
- if (pageLockOnly) {
- SM_ERROR(TYPE_USER, esmCAUSEDPHANTOMDEADLOCK);
- for (i = 1; i < DLtrailCnt; i++) {
- holderTrans = DLtrail[i]->headerList.transRec;
- while (LIST_NOT_EMPTY(&holderTrans->lockWaitList)) {
- holderEntry = (LOCKENTRY*)
- FIRST_LIST_ELEMENT(& holderTrans->lockWaitList);
- CHECK_LOCKENTRY_MAGIC(holderEntry);
- freeLockWaiter(holderEntry, esmPHANTOMDEADLOCK);
- }
- }
- }
- else {
- SM_ERROR(TYPE_USER, esmLOCKCAUSEDDEADLOCK);
- DLtrailCnt = 0;
- }
- }
-
- return detected;
- }
-